// 集采字典管理
import { getDictTag, getJcDict, getStoreDict } from '@fl/api/man-api/jc-dict'
import { store } from '@fl/store/index'
import { useUserStoreWithOut } from '@fl/store/modules/user'
import { defineStore } from 'pinia'

export const useDict = defineStore('dict', {
    actions: {
        add(key: string, value: any) {
            this.dicts[key] = value
        },
        dictTrans(key: string, value: any, getLabel = true) {
            if (Array.isArray(value)) {
                const labels: any = []
                value.forEach((val: any) => {
                    let tgt = this.dicts[key].find((v: any) => v.value === `${val}`)?.label || val
                    if (!getLabel)
                        tgt = this.dicts[key].find((v: any) => v.label === `${val}`)?.value || val

                    labels.push(tgt)
                })
                return labels.join(',')
            }
            let target = this.dicts[key].find((v: any) => v.value === `${value}`)?.label || value
            if (!getLabel)
                target = this.dicts[key].find((v: any) => v.label === `${value}`)?.value || value

            return target
        },
        async init(keys: string[], labelKey = 'dictLabel', valueKey = 'dictValue') {
            if (!keys?.length)
                return

            const userStore: any = await useUserStoreWithOut()

            // 判断当前用户身份 是否运营端
            const isAdmin = userStore.getUserInfo.iAmAdmin

            let arr: any = []

            if (isAdmin) {
                arr = keys.map(async (key: any) => {
                    // TODO 当前接口不支持商城token
                    return await getDictTag(key)
                })
            }
            else {
                arr = keys.map(async (key: any) => {
                    // 采购端 供应端查询字典接口
                    return await getStoreDict(key)
                })
            }

            if (arr.length) {
                Promise.all(arr).then((res: any) => {
                    keys.forEach((v: any, idx: number) => {
                        // getDictTag 返回数据不存在rows字段
                        const target = res[idx]?.rows ?? res[idx]
                        this.dicts[v] = target.map((v: any) => {
                            return {
                                ...v,
                                label: v[labelKey],
                                value: v[valueKey],
                            }
                        })
                    })
                }).catch((error) => {
                    console.log(error)
                })
            }
        },
        // 集采首页 无需token
        async initJc(keys: string[], labelKey = 'dictLabel', valueKey = 'dictValue') {
            if (!keys?.length)
                return

            let arr: any = []

            arr = keys.map(async (key: any) => {
                return await getJcDict(key)
            })

            if (arr.length) {
                Promise.all(arr).then((res: any) => {
                    keys.forEach((v: any, idx: number) => {
                        // getDictTag 返回数据不存在rows字段
                        const target = res[idx]?.rows ?? res[idx]
                        this.dicts[v] = target.map((v: any) => {
                            return {
                                ...v,
                                label: v[labelKey],
                                value: v[valueKey],
                            }
                        })
                    })
                }).catch((error) => {
                    console.log(error)
                })
            }
        },
    },
    getters: {
        getDicts(): any {
            return this.dicts
        },
    },
    persist: true,
    state: (): any => ({
        dicts: {
            // dict_yewulb: [],
        },
    }),
})

export function useStoreDict() {
    return useDict(store)
}
